链表长度的测量
链表长度的测量
需要借助一个游标来记录链表的元素个数
class SingleLinkList(object):
def __init__(self, node=None):
# head: 首结点
self.head = node
# 判断链表是否为空
......
# 获取链表长度
def length(self):
# 游标记录当前所在的位置
cur = self.head
# 记录链表的长度
count = 0
while cur is not None:
cur = cur.next
count += 1
return count